<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        @click="isShow = !isShow"
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        >{{ isShow ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(item, index) in userChannel"
        :key="item.id"
        @click="onUserChannelClick(index, item)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-if="item.name !== '推荐' && isShow"
        ></van-icon>
        <span
          :class="activeIndex === index ? 'text active' : 'text'"
          slot="text"
          >{{ item.name }}</span
        >
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        @click="onAddChannel(item)"
        class="grid-item"
        icon="plus"
        :text="item.name"
        v-for="item in recommendChannels"
        :key="item.id"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel
} from "@/api/channel";
import { setItem } from "@/utils/storage";
export default {
  name: "ChannelEdit",
  components: {},
  props: {
    activeIndex: {
      type: Number,
      required: true
    },
    userChannel: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      allChannels: [],
      isShow: false
    };
  },
  computed: {
    recommendChannels() {
      let list = this.allChannels.filter(item => {
        return !this.userChannel.find(element => element.id == item.id);
      });
      return list;
    }
  },
  watch: {},
  created() {
    this.loadAllChannels();
  },
  mounted() {},
  methods: {
    // 获取全部频道列表
    async loadAllChannels() {
      try {
        let { data: res } = await getAllChannels();
        console.log(res);
        this.allChannels = res.data.channels;
      } catch (error) {
        this.$toast.fail("获取全部频道列表失败");
      }
    },
    // 添加到我的用户频道
    async onAddChannel(val) {
      try {
        // 判断当前是否是登录状态
        if (this.$store.state.user) {
          // 登录状态 发送请求
          await addUserChannel([
            {
              id: val.id,
              seq: this.userChannel.length
            }
          ]);
          this.userChannel.push(val);
        } else {
          // 没有登录状态，添加到本地存储
          this.userChannel.push(val);
          setItem("USER_CHANNELS", this.userChannel);
        }
      } catch (error) {
        this.$toast.fail("添加用户频道失败");
      }
    },
    // 点击我的用户频道事件
    async onUserChannelClick(idx, val) {
      // 判断当前是什么状态
      if (this.isShow) {
        if (val.name == "推荐") return;
        try {
          // 判断当前是否是登录状态
          if (this.$store.state.user) {
            // 登录状态 发送请求
            await deleteUserChannel(val.id);

            // 当前是编辑列表
            if (this.activeIndex >= idx) {
              this.$emit("update:active-index", this.activeIndex - 1);
            }
            this.userChannel.splice(idx, 1);
          } else {
            // 当前是编辑列表
            if (this.activeIndex >= idx) {
              this.$emit("update:active-index", this.activeIndex - 1);
            }
            this.userChannel.splice(idx, 1);
            // 没有登录状态，添加到本地存储
            setItem("USER_CHANNELS", this.userChannel);
          }
        } catch (error) {
          this.$toast.fail("删除用户频道失败");
        }
      } else {
        // 当前是关闭弹框，跳转
        console.log(idx);
        this.$emit("close");
        this.$emit("update:active-index", idx);
      }
    }
  }
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
